﻿using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;

namespace BizElements.Generator.EntityComponents
{
    /// <summary>
    /// Provides common operations for array and list manipulation (conversions, sets...).
    /// </summary>
    public static class ArrayUtil
    {
        /// <summary>
        /// Searches for the specified object and returns the zero-based index of the first occurrence within the entire collection.
        /// </summary>
        /// <param name="collection">A collection.</param>
        /// <param name="item">The object to locate in the collection. The value can be <b>null</b> for reference types.</param>
        /// <returns>The zero-based index of the first occurrence of item within the entire collection, if found; otherwise, –1.</returns>
        public static int IndexOf(IEnumerable collection, object item)
        {
            int currIndex = 0;
            foreach (object currItem in collection)
            {
                if (item == currItem)
                    return currIndex;

                currIndex++;
            }

            return -1;
        }

        /// <summary>
        /// Retrieves all the elements that match the conditions defined by the specified predicate.
        /// </summary>
        /// <typeparam name="TList">Type of the list which is returned from the method.</typeparam>
        /// <typeparam name="TElement">Type of elements contained in the collection.</typeparam>
        /// <param name="collection">A collection.</param>
        /// <param name="match">The Predicate(TElement) delegate that defines the conditions of the elements to search for.</param>
        /// <returns>A list containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty list.</returns>
        internal static TList FindAll<TList, TElement>(IEnumerable collection, Predicate<TElement> match)
            where TList : IList, new()
        {
            TList matches = new TList();
            foreach (TElement obj in collection)
            {
                if (match(obj))
                    matches.Add(obj);
            }

            return matches;
        }

        /// <summary>
        /// Checks whether the specified array is a null reference or an empty array.
        /// </summary>
        /// <param name="array">Array to check.</param>
        /// <returns><b>true</b> if the provided array is <b>null</b> or empty; otherwise, false.</returns>
        internal static bool IsNullOrEmpty(Array array)
        {
            bool isNullOrEmtpy = (array == null) || (array.Length == 0);
            return isNullOrEmtpy;
        }

        /// <summary>
        /// Checks whether the specified collection is a null reference or an empty collection.
        /// </summary>
        /// <param name="collection">Collection to check.</param>
        /// <returns><b>true</b> if the provided collection is <b>null</b> or empty; otherwise, false.</returns>
        internal static bool IsNullOrEmpty(IEnumerable collection)
        {
            if (collection == null)
                return true;

            IEnumerator enumerator = collection.GetEnumerator();
            bool isEmpty = !enumerator.MoveNext();
            return isEmpty;
        }
    }
}